/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package flp.gui.listener;

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import javax.swing.JTextField;

/**
 *
 * @author marek
 */
public class IntegerFieldFocusListener extends FocusAdapter
{
    @Override
    public void focusGained(FocusEvent e)
    {
        JTextField textField = (JTextField)e.getSource();
        textField.selectAll();
    }

    @Override
    public void focusLost(FocusEvent e)
    {
        JTextField textField = (JTextField)e.getSource();
        String content = textField.getText();
        if (content.length() == 0)
        {
            textField.setText("0");
            return;
        }

        try
        {
            textField.setText(Integer.toString(Integer.parseInt(content)));
        }
        catch (NumberFormatException nfe)
        {
            System.err.println(nfe.getStackTrace());
            textField.requestFocusInWindow();
        }
    }
}
